package jzqb.controller;

import com.alibaba.fastjson.JSONObject;
import haha.life.common.CutPageBean;
import haha.life.normal.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jzqb.bo.NoticeBo;
import jzqb.common.Result;
import jzqb.service.INoticeService;
import jzqb.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Description:个人中中心-->我的消息
 *
 * @author BaiGe
 * @date: 2021/4/21 15:11
 * @Version 1.0
 */
@Api(tags = "通知公告-前台")
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    @ApiOperation(value = "展示我的消息")
    @PostMapping(value = "/queryNotices")
    public Result queryNotices(@RequestBody NoticeBo noticeBo, HttpServletRequest request) {
        noticeBo.setStart(noticeBo.getPage(), noticeBo.getPageSize());
        noticeBo.setUserId(TokenUtils.getCurrentUserId(request));
        CutPageBean<NoticeVo> cutPageBean = noticeService.showNoticeFront(noticeBo);
        return Result.succ(cutPageBean);
    }


    @ApiOperation(value = "修改已读列表")
    @PostMapping(value = "/updateReadUser")
    public Result updateReadUser(@RequestBody String params, HttpServletRequest request) {
        JSONObject jsonObject = JSONObject.parseObject(params);
        Integer noticeId = jsonObject.getInteger("noticeId");
        Integer readState = jsonObject.getInteger("readState");
        String userId = TokenUtils.getCurrentUserId(request);
        boolean bl = false;
        if (readState == 0) { //未读
            bl = noticeService.updateReadUser(noticeId, userId);
        }
        return Result.succ(bl);
    }

    @ApiOperation(value = "红点提示--未读数量")
    @GetMapping("/getUnReadNoticeNum")
    public Result getUnReadNoticeNum(HttpServletRequest request) {
        NoticeBo noticeBo=new NoticeBo();
        noticeBo.setUserId(TokenUtils.getCurrentUserId(request));
        int num = (int) noticeService.getUnReadNoticeNum(noticeBo);
        return Result.succ(num);
    }

}
